﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.ComponentModel;
using Shoozla.DataModel;

namespace Shoozla.ViewModel
{
    public enum CoverCollections
    {
        Album,
        Song,
        Artist
    }

    class SearchingNagScreenViewModel : ViewModelBase
    {

        private MultithreadingObservableCollection<CoverThumb> CurrentCollectionSelected;

        public MultithreadingObservableCollection<CoverThumb> AlbumCoversFound { get; private set; }
        public MultithreadingObservableCollection<CoverThumb> SongCoversFound { get; private set; }
        public MultithreadingObservableCollection<CoverThumb> ArtistCoversFound { get; private set; }

         
        public SearchingNagScreenViewModel():base() {
            AlbumCoversFound = new MultithreadingObservableCollection<CoverThumb>();
            SongCoversFound = new MultithreadingObservableCollection<CoverThumb>();
            ArtistCoversFound = new MultithreadingObservableCollection<CoverThumb>();
            ChangeCurrentCollection(CoverCollections.Album);
        }

        #region Commands

        private RelayCommand _CmdClose;
        public RelayCommand CmdClose
        {
            get
            {
                if (_CmdClose == null)
                {
                    _CmdClose = new RelayCommand(param => DoCmdClose(), param =>
                    {
                        if (App.CurrentAppStatus == App.AppStatus.Idle)
                            return true;
                        else
                            return false;
                    });
                }
                return _CmdClose;
            }
        }

        private RelayCommand _CmdApply;
        public RelayCommand CmdApply
        {
            get
            {
                if (_CmdApply == null)
                {
                    _CmdApply = new RelayCommand(param => DoCmdApply(), param => {
                        if (App.CurrentAppStatus == App.AppStatus.Idle)
                            return true;
                        else
                            return false;
                    });
                }
                return _CmdApply;
            }
        }

        private RelayCommand _CmdCancel;
        public RelayCommand CmdCancel
        {
            get
            {
                if (_CmdCancel == null)
                {
                    _CmdCancel = new RelayCommand(param => DoCmdCancel(), (c) => {
                        if (App.CurrentAppStatus != App.AppStatus.Idle)
                            return true;
                        else
                            return false;
                    });
                }
                return _CmdCancel;
            }
        }

        private RelayCommand _CmdPreviewSelectAll;
        public RelayCommand CmdPreviewSelectAll
        {
            get
            {
                if (_CmdPreviewSelectAll == null)
                {
                    _CmdPreviewSelectAll = new RelayCommand(param => DoCmdPreviewSelectAll(), param => CanExecuteUI());
                }
                return _CmdPreviewSelectAll;
            }
        }

        private RelayCommand _CmdPreviewSelectNone;
        public RelayCommand CmdPreviewSelectNone
        {
            get
            {
                if (_CmdPreviewSelectNone == null)
                {
                    _CmdPreviewSelectNone = new RelayCommand(param => DoCmdPreviewSelectNone(), param => CanExecuteUI());
                }
                return _CmdPreviewSelectNone;
            }
        }

        #endregion

        #region Do Commands

        private void DeleteCovers(IEnumerable<CoverThumb> selectedCovers)
        {
            foreach (CoverThumb s in selectedCovers)
            {
                if (App.CoverImageCache.ContainsCover(s.CachedUrl.AbsoluteUri))
                {
                    App.CoverImageCache.RemoveCover(s.CachedUrl.AbsoluteUri);
                    //MessageBox.Show("REMOVED: " + s.DisplayString);
                }
            }
        }

        private void DoCmdApply()
        {
            //i do remove all not selected covers from the cache
           var selectedAlbumCovers = from c in this.AlbumCoversFound
                                 where !c.IsSelected
                                 select c;

           var selectedSongCovers = from c in this.SongCoversFound
                                     where !c.IsSelected
                                     select c;

           var selectedArtistCovers = from c in this.ArtistCoversFound
                                     where !c.IsSelected
                                     select c;

           DeleteCovers(selectedAlbumCovers);
           DeleteCovers(selectedSongCovers);
           DeleteCovers(selectedArtistCovers);

           App.ApplyFoundCoversToSongs();
           DoCmdClose();
        }

        private void DoCmdCancel()
        {
            if (App.CurrentAppStatus != App.AppStatus.Idle)
            {
                var res = MessageBox.Show("Do you want to stop the search?", "Confirmation", MessageBoxButton.YesNo, MessageBoxImage.Question);
                if (res == MessageBoxResult.Yes)
                    App.StopWorkingThreads();
            }
        }

        private void DoCmdClose()
        {
            App.SearchingNagScreenWindow.CloseWindow();
        }

        private void DoCmdPreviewSelectAll()
        {
            foreach (CoverThumb c in CurrentCollectionSelected)
                c.IsSelected = true;        
        }

        private void DoCmdPreviewSelectNone()
        {
            foreach (CoverThumb c in CurrentCollectionSelected)
                c.IsSelected = false;  
        }

        #endregion

        public void ChangeCurrentCollection(CoverCollections current)
        {
            switch (current)
            {
                case CoverCollections.Album: { CurrentCollectionSelected = AlbumCoversFound; break;}
                case CoverCollections.Song: { CurrentCollectionSelected = SongCoversFound; break;}
                case CoverCollections.Artist: { CurrentCollectionSelected = ArtistCoversFound;  break; }
            }
        }

        public void ClearCovers()
        {
            AlbumCoversFound.Clear();
            SongCoversFound.Clear();
            ArtistCoversFound.Clear();

        }

        public void AddSongToDisplay(CoverThumb m, SearchMethod method)
        {
            m.IsSelected = true;
            if(method == SearchMethod.Album)
                AlbumCoversFound.Add(m);
            if (method == SearchMethod.Song)
                SongCoversFound.Add(m);
            if (method == SearchMethod.Artist)
                ArtistCoversFound.Add(m);
        }
    }
}
